package com.sxt.study.email.controller;

import com.sxt.study.email.model.MailResult;
import com.sxt.study.email.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author songj
 * @date 2019/8/8 15:06
 */
@RestController
@Slf4j
public class MailController {
    @Autowired
    private MailService mailService;

    @RequestMapping("/sendSimpleMail")
    public MailResult sendSampleEmail(String to,String subject,String content) {
        MailResult result = new MailResult();
        if (StringUtils.isEmpty(to) || !to.contains("@")) {
            result.setRspCode("01");
            result.setRspMsg("收件人邮件格式不正确");
            return result;
        }
        if (StringUtils.isEmpty(subject) || StringUtils.isEmpty(content)) {
            result.setRspCode("02");
            result.setRspMsg("邮件主题或内容不能为空");
            return result;
        }

        try {
            mailService.sendSimpleMail(to, subject, content);
        } catch (Exception e) {
            result.setRspMsg("03");
            result.setRspMsg("邮件发送异常");
            log.error("邮件发送异常",e);
        }
        return result;
    }
}
